# Instalação de pacotes
pacotes <- c('titanic', # carrega a base original titanic_treino
'tidyverse', # Pacote básico de datawrangling
'rpart', # Biblioteca de árvores
'rpart.plot', # Conjunto com Rpart, plota a parvore
'gtools', # funções auxiliares como quantcut,
'Rmisc', # carrega a função sumarySE para a descritiva
'scales', # importa paletas de cores
'caret', # Funções úteis para machine learning
'plotly' # Biblioteca p/ plotagem de gráficos
)
if(sum(as.numeric(!pacotes %in% installed.packages())) != 0){
instalador <- pacotes[!pacotes %in% installed.packages()]
for(i in 1:length(instalador)) {
install.packages(instalador, dependencies = T)
break()}
sapply(pacotes, require, character = T)
} else {
sapply(pacotes, require, character = T)
}
## titanic tidyverse rpart rpart.plot gtools Rmisc scales
## TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## caret plotly
## TRUE TRUE
load("C:/Users/Vinicius/Desktop/MBA/Outros Modelos de Machine Learning/.RData")
titanic %>% head
## Survived Pclass Sex Age SibSp Parch Fare Embarked
## 1 N 3 male 22.00000 1 0 7.2500 S
## 2 Y 1 female 38.00000 1 0 71.2833 C
## 3 Y 3 female 26.00000 0 0 7.9250 S
## 4 Y 1 female 35.00000 1 0 53.1000 S
## 5 N 3 male 35.00000 0 0 8.0500 S
## 6 N 3 male 29.69912 0 0 8.4583 Q
summary(titanic)
## Survived Pclass Sex Age SibSp
## N:549 Min. :1.000 female:314 Min. : 0.42 Min. :0.000
## Y:342 1st Qu.:2.000 male :577 1st Qu.:22.00 1st Qu.:0.000
## Median :3.000 Median :29.70 Median :0.000
## Mean :2.309 Mean :29.70 Mean :0.523
## 3rd Qu.:3.000 3rd Qu.:35.00 3rd Qu.:1.000
## Max. :3.000 Max. :80.00 Max. :8.000
## Parch Fare Embarked
## Min. :0.0000 Min. : 0.00 C:168
## 1st Qu.:0.0000 1st Qu.: 7.91 Q: 77
## Median :0.0000 Median : 14.45 S:646
## Mean :0.3816 Mean : 32.20
## 3rd Qu.:0.0000 3rd Qu.: 31.00
## Max. :6.0000 Max. :512.33
titanic %>% str
## 'data.frame': 891 obs. of 8 variables:
## $ Survived: Factor w/ 2 levels "N","Y": 1 2 2 2 1 1 1 1 2 2 ...
## $ Pclass : int 3 1 3 1 3 3 1 3 3 2 ...
## $ Sex : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
## $ Age : num 22 38 26 35 35 ...
## $ SibSp : int 1 1 0 1 0 0 0 3 0 1 ...
## $ Parch : int 0 0 0 0 0 0 0 1 2 0 ...
## $ Fare : num 7.25 71.28 7.92 53.1 8.05 ...
## $ Embarked: Factor w/ 3 levels "C","Q","S": 3 1 3 3 3 2 3 3 3 1 ...
graf1 <- plot_ly(x=titanic$Sex, type = 'histogram') %>% layout(title = 'Sexo')
graf1
graf2 <- hist(titanic$Age, col = 'red', main = "Histograma da Idade")
graf3 <- hist(titanic$Pclass, col = 'lightgray', main = "Histograma da Classe")
arvore <- rpart(Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked,
data=titanic,
parms = list(split = 'gini'), # podemos trocar para 'information'
method='class' # Essa opção indica que a resposta é qualitativa
)
rpart.plot::rpart.plot(arvore
)
prob = predict(arvore, titanic)
class = prob[,2]>.5
tab <- table(class, titanic$Survived)
tab
##
## class N Y
## FALSE 498 98
## TRUE 51 244
df <- as.data.frame(prob)
survived <- filter(df, Y>0.5)
not_survived <- filter(df, Y<0.5)
acc <- (tab[1,1] + tab[2,2])/ sum(tab)
print(paste0('O modelo apresentou ', nrow(survived) ,' sobreviventes'))
## [1] "O modelo apresentou 295 sobreviventes"
print(paste0('O modelo apresentou ', nrow(not_survived) ,' não sobreviventes'))
## [1] "O modelo apresentou 596 não sobreviventes"
print(paste0('A acurácia foi de:', acc))
## [1] "A acurácia foi de:0.832772166105499"